<div class="events index">
    <?php if ($event_time == 'upcoming') { ?>
    <h2><?php __('Upcoming Events'); ?></h2>
    <?php } else {?>
    <h2><?php __('Past Events'); ?></h2>
    <?php } ?>
    <table>
        <tr>
            <td class="actions">
                <?php echo $this->Html->link(__('Upcoming', true), array('controller' => 'events', 'action' => 'index','upcoming')); ?>
                <?php echo $this->Html->link(__('Past', true), array('controller' => 'events', 'action' => 'index','past')); ?>
            </td>
        </tr>
    </table>

    <?php if ($events) { ?>
    <?php foreach ($events as $event) { ?>
        <h3>
        <?php 
        	echo $event['Event']['event_name'];
        	if ($event_time == 'past') {
        		echo $this->Html->link("[Read Reviews]", array('controller'=>'events', 'action' => 'reviews', 'read', $event['Event']['id']));
        		
        		/* if curr user attended the event and has not written a review, he can write one */
        		foreach ($attend_events as $attend_event) {
        			if ($attend_event['AttendEvent']['event_id'] == $event['Event']['id'] && $attend_event['AttendEvent']['user_id'] == $user['User']['id']) {
        				if ($attend_event['AttendEvent']['feedback']==null) {
        					echo $this->Html->link("[Write a Review]", array('controller'=>'events', 'action' => 'reviews', 'write', $event['Event']['id']));
        				}
        				break;
        			}        				
    			}
        	}	
        	?>
        </h3>
        <div class="event_listing">
            <div class="event_listing_left">
                <?php
                    if (file_exists('img/event/' . $event['Event']['id'] . '.jpg')) {
                        echo $this->Html->image('event/' . $event['Event']['id'] . '.jpg', array('width' => '90'));
                    } else {
                        echo $this->Html->image('default_event.jpg', array('width' => '90'));
                    }
                ?>
            </div>
            <div class="event_listing_right">
                <p><span class="small_title">Location: </span><?php echo $event['Event']['venue_addr1'] . ', ' . $event['Event']['venue_city'] . ', ' . $event['Event']['venue_state'] . ' ' . $event['Event']['venue_postal']; ?></p>
                <p><span class="small_title">Start Time: </span><?php echo $event['Event']['event_start']; ?></p>
                <p><span class="small_title">End Time: </span><?php echo $event['Event']['event_end']; ?></p>
                <p><span class="small_title">Details: </span><?php echo $event['Event']['description']; ?></p>
                <p><span class="small_title">Type: </span><?php echo $event['Event']['event_type']; ?></p>
                
                <?php 
                if ($event['Event']['event_type'] == 'public') { 
                	
                	$decision_is_made = false;
                	foreach ($attend_events as $attend_event) {
                		
                		if ($attend_event['AttendEvent']['event_id'] == $event['Event']['id'] && $attend_event['AttendEvent']['decision'] != null) {
                			$decision_is_made = true;
                			$decison = '';
                			if ($attend_event['AttendEvent']['decision'])
                				$decision = 'Yes';
                			else 
                				$decision = 'No';
                			echo '<p><span class="small_title">Attending? '. $decision .'</span>';
                			break;
                		}
                	}
                	
                	if ($decision_is_made == false && $event_time == 'upcoming' ) {	
                		echo $form->create('Event', array('action'=>'attend_events/'.$event['Event']['id']));
                		$options = array(true => 'Yes', false => 'No');
                		echo $form->radio('Are you attending?', $options);
                		echo $form->end('Submit');
                	}
                } 
                ?>
                
                <?php 
                if ($event['Event']['event_type'] == 'invite' && $status['Invite']['status'] == null){
               		//echo $event['Event']['id'];
            		echo $this->Form->create('User', array('action'=> 'invite_choice/' .$event['Event']['id']));
               		$options = array(true => 'Accept', false => 'Reject');
               		echo $this->Form->radio('status', $options);
               		echo $this->Form->end('Submit');
               	} 
               	elseif ($event['Event']['event_type'] == 'invite' && $status['Invite']['status'] == 1) {
               		echo '<h3>You have accepted the invite to this event!</h3>';
               	}
                ?>
                
            </div>
            <div style="clear:both;"></div>
        </div>
    <?php } ?>
    <?php } else { ?>
        <?php if ($event_time == 'upcoming') { ?>
            <h3>You have no upcoming events in this section</h3>
        <?php } else {?>
            <h3>You have no past events in this section</h3>
        <?php } ?>
    <?php } ?>
    <div class="paging" style="background-color: #EEECF1;">
        <center>
            <?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class' => 'disabled')); ?>
            |
            <?php echo $this->Paginator->numbers(); ?>
            |
            <?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled')); ?>
        </center>
        <br/>
        <center>
            <?php 
            echo $this->Paginator->counter(array(
                'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
            )); 
            ?>
        </center>
    </div>    
</div>

